import type { AuthInfo } from '@/types/resource'
import http from './http'
import { apiMsgProp } from '@/utils/global'

const uploadChunkService = {
    // 检查是否授权
    checkAuth: async (): Promise<AuthInfo> => {
        const res = await http.get('/course/resource/panopto/authorize')
        if (res.data.code !== 200) return Promise.reject(res.data)
        return res.data.data
    },

    // 直接上传视频到Panopto
    uploadVideo: async (formData: FormData): Promise<any> => {
        const res = await http.post('/course/resource/panopto/upload', formData, {
            headers: {
                'Content-Type': 'multipart/form-data',
            },
            timeout: undefined,
        })
        if (res.data.code !== 200) return Promise.reject(res.data)
        return res.data.data
    },

    // 取消上传视频
    cancelUploadVideo: async (video_id: number): Promise<void> => {
        const res = await http.post(`course/resource/panopto/upload/${video_id}/cancel`)
        if (res.data.code !== 200) return Promise.reject(res.data)
        return res.data.data
    },

    // 获取视频状态和访问地址
    getVideoStatus: async (panopto_video_id: number): Promise<any> => {
        const res = await http.get(`/course/resource/panopto/upload-status/${panopto_video_id}`)
        if (res.data.code !== 200) return Promise.reject(res.data)
        return res.data.data
    },

    // 初始化分片上传
    chunkInit: async (data: any): Promise<any> => {
        const res = await http.post('/course/resource/chunk/init', data)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
        return res.data.data
    },
    // 上传分片文件
    chunkUpload: async (formData: FormData, cancelToken: any, callback: any): Promise<void> => {
        const res = await http.post(`/course/resource/chunk/upload`, formData, {
            cancelToken: cancelToken,
            headers: {
                'Content-Type': 'multipart/form-data',
                'Content-Encoding': 'identity',
            },
            timeout: 60 * 60 * 1000,
            onUploadProgress: progressEvent => {
                return callback(progressEvent)
            },
        })
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
        return res.data.data
    },
    // 完成上传
    chunkComplete: async (data: any): Promise<any> => {
        const res = await http.post('/course/resource/chunk/complete', data)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
        return res.data.data
    },
    // 取消上传
    chunkCancel: async (data: object): Promise<void> => {
        const res = await http.post('/course/resource/chunk/cancel', data)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
        return res.data.data
    },
    // 获取上传进度
    chunkProgress: async (uploadId: string): Promise<void> => {
        const res = await http.get(`/course/resource/chunk/progress?upload_id=${uploadId}`)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
        return res.data.data
    },
    // 检查分片
    chunkCheck: async (uploadId: string, chunkNumber: number): Promise<any> => {
        const res = await http.get(`/course/resource/chunk/check?upload_id=${uploadId}&chunk_number=${chunkNumber}`)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
        return res.data.data
    },
}

export default uploadChunkService
